noble-hashes
Fast, secure & minimal JS implementation of SHA2, SHA3, RIPEMD, BLAKE2/3, HMAC, HKDF, PBKDF2 & Scrypt.
- noble family, zero dependencies
- 🔻 Helps JS bundlers with lack of entry point; ensures small size of your app
- 🔁 No unrolled loops: makes it much easier to verify and reduces source code size 2-5x
- 🏎 Ultra-fast, hand-optimized for caveats of JS engines
- 🔍 Unique tests ensure correctness: chained tests, sliding window tests, DoS tests
- 🧪 Differential fuzzing ensures even more correctness with cryptofuzz
- 🔑 Scrypt supports
n: 2**22
with 4GB arrays while other implementations crash on 2**21
or even 2**20
, maxmem
security param, onProgress
callback - 🦘 SHA3 supports Keccak, TupleHash, KangarooTwelve and MarsupilamiFourteen
- All primitives are just ~2KLOC / 41KB minified / 14KB gzipped. SHA256-only is 240LOC / 7KB minified / 3KB gzipped
The library's initial development was funded by Ethereum Foundation.
This library belongs to noble crypto
noble-crypto — high-security, easily auditable set of contained cryptographic libraries and tools.
- No dependencies, small files
- Easily auditable TypeScript/JS code
- Supported in all major browsers and stable node.js versions
- All releases are signed with PGP keys
- Check out all libraries:
secp256k1,
ed25519,
bls12-381,
hashes
Usage
Use NPM in node.js / browser, or include single file from
GitHub's releases page:
npm install @noble/hashes
The library does not have an entry point. It allows you to select specific primitives and drop everything else. If you only want to use sha256, just use the library with rollup or other bundlers. This is done to make your bundles tiny.
import { sha256 } from '@noble/hashes/lib/sha256';
console.log(sha256(new Uint8Array([1, 2, 3])));
console.log(sha256('abc')));
const { sha512, sha512_256, sha384 } = require('@noble/hashes/lib/sha512');
const {
sha3_224, sha3_256, sha3_384, sha3_512,
keccak_224, keccak_256, keccak_384, keccak_512,
shake128, shake256
} = require('@noble/hashes/lib/sha3');
const {
cshake128, cshake256, kmac128, kmac256,
k12, m14,
tuplehash256, parallelhash256, keccakprg
} = require('@noble/hashes/lib/sha3-addons');
const { ripemd160 } = require('@noble/hashes/lib/ripemd160');
const { blake3 } = require('@noble/hashes/lib/blake3');
const { blake2b } = require('@noble/hashes/lib/blake2b');
const { blake2s } = require('@noble/hashes/lib/blake2s');
const { hmac } = require('@noble/hashes/lib/hmac');
const { hkdf } = require('@noble/hashes/lib/hkdf');
const { pbkdf2, pbkdf2Async } = require('@noble/hashes/lib/pbkdf2');
const { scrypt, scryptAsync } = require('@noble/hashes/lib/scrypt');
const { bytesToHex as toHex } = require('@noble/hashes/lib/utils');
console.log(toHex(sha256('abc')));
API
All hash functions:
- can be called directly, with
Uint8Array
. - return
Uint8Array
- can receive
string
, which is automatically converted to Uint8Array
via utf8 encoding (not hex) - support hashing 4GB of data per update on 64-bit systems (unlimited with streaming)
function hash(message: Uint8Array | string): Uint8Array;
hash(new Uint8Array([1, 3]));
hash('string') == hash(new TextEncoder().encode('string'));
All hash functions can be constructed via hash.create()
method:
- the result is
Hash
subclass instance, which has update()
and digest()
methods digest()
finalizes the hash and makes it no longer usable
hash
.create()
.update(new Uint8Array([1, 3]))
.digest();
Some hash functions can also receive options
object, which can be either passed as a:
- second argument to hash function:
blake3('abc', { key: 'd', dkLen: 32 })
- first argument to class initializer:
blake3.create({ context: 'e', dkLen: 32 })
Modules
SHA2 (sha256, sha384, sha512, sha512_256)
import { sha256 } from '@noble/hashes/lib/sha256.js';
const h1a = sha256('abc');
const h1b = sha256
.create()
.update(Uint8Array.from([1, 2, 3]))
.digest();
import { sha512 } from '@noble/hashes/lib/sha512.js';
const h2a = sha512('abc');
const h2b = sha512
.create()
.update(Uint8Array.from([1, 2, 3]))
.digest();
import { sha512_256 } from '@noble/hashes/lib/sha512.js';
const h3a = sha512_256('abc');
const h3b = sha512_256
.create()
.update(Uint8Array.from([1, 2, 3]))
.digest();
import { sha384 } from '@noble/hashes/lib/sha512.js';
const h4a = sha384('abc');
const h4b = sha384
.create()
.update(Uint8Array.from([1, 2, 3]))
.digest();
See RFC 4634 and the paper on SHA512/256.
SHA3 (FIPS, SHAKE, Keccak)
import {
sha3_224,
sha3_256,
sha3_384,
sha3_512,
keccak_224,
keccak_256,
keccak_384,
keccak_512,
shake128,
shake256,
} from '@noble/hashes/lib/sha3.js';
const h5a = sha3_256('abc');
const h5b = sha3_256
.create()
.update(Uint8Array.from([1, 2, 3]))
.digest();
const h6a = keccak_256('abc');
const h7a = shake128('abc', { dkLen: 512 });
const h7b = shake256('abc', { dkLen: 512 });
See (FIPS PUB 202, Website).
Check out the differences between SHA-3 and Keccak
SHA3 Addons (cSHAKE, KMAC, TupleHash, ParallelHash, KangarooTwelve, MarsupilamiFourteen)
import {
cshake128,
cshake256,
kmac128,
kmac256,
k12,
m14,
tuplehash128,
tuplehash256,
parallelhash128,
parallelhash256,
keccakprg,
} from '@noble/hashes/lib/sha3-addons.js';
const h7c = cshake128('abc', { personalization: 'def' });
const h7d = cshake256('abc', { personalization: 'def' });
const h7e = kmac128('key', 'message');
const h7f = kmac256('key', 'message');
const h7h = k12('abc');
const h7g = m14('abc');
const h7i = tuplehash128(['ab', 'c']);
const h7j = parallelhash128('abc', { blockLen: 8 });
const p = keccakprg(254);
p.feed('test');
const rand1b = p.fetch(1);
- Full NIST SP 800-185: cSHAKE, KMAC, TupleHash, ParallelHash + XOF variants
- 🦘 K12 (KangarooTwelve Paper, RFC Draft) and M14 aka MarsupilamiFourteen are basically parallel versions of Keccak with reduced number of rounds (same as Blake3 and ParallelHash).
- KeccakPRG: Pseudo-random generator based on Keccak
RIPEMD-160
import { ripemd160 } from '@noble/hashes/lib/ripemd160.js';
const hash8 = ripemd160('abc');
const hash9 = ripemd160()
.create()
.update(Uint8Array.from([1, 2, 3]))
.digest();
See RFC 2286, Website
BLAKE2b, BLAKE2s
import { blake2b } from '@noble/hashes/lib/blake2b.js';
import { blake2s } from '@noble/hashes/lib/blake2s.js';
const h10a = blake2s('abc');
const b2params = { key: new Uint8Array([1]), personalization: t, salt: t, dkLen: 32 };
const h10b = blake2s('abc', b2params);
const h10c = blake2s
.create(b2params)
.update(Uint8Array.from([1, 2, 3]))
.digest();
See RFC 7693, Website.
BLAKE3
import { blake3 } from '@noble/hashes/lib/blake3.js';
const h11 = blake3('abc', { dkLen: 256, key: 'def', context: 'fji' });
See Website.
HMAC
import { hmac } from '@noble/hashes/lib/hmac.js';
import { sha256 } from '@noble/hashes/lib/sha256.js';
const mac1 = hmac(sha256, 'key', 'message');
const mac2 = hmac.create(sha256, Uint8Array.from([1, 2, 3])).update(Uint8Array.from([4, 5, 6]).digest();
Matches RFC 2104.
HKDF
import { hkdf } from '@noble/hashes/lib/kdf.js';
import { sha256 } from '@noble/hashes/lib/sha256.js';
import { randomBytes } from '@noble/hashes/utils.js';
const inputKey = randomBytes(32);
const salt = randomBytes(32);
const info = 'abc';
const dkLen = 32;
const hk1 = hkdf(sha256, inputKey, salt, info, dkLen);
import { hkdf_extract, hkdf_expand } from '@noble/hashes/lib/kdf.js';
import { sha256 } from '@noble/hashes/lib/sha256.js';
const prk = hkdf_extract(sha256, inputKey, salt);
const hk2 = hkdf_expand(sha256, prk, info, dkLen);
Matches RFC 5869.
PBKDF2
import { pbkdf2, pbkdf2Async } from '@noble/hashes/lib/pbkdf2.js';
import { sha256 } from '@noble/hashes/lib/sha256.js';
const pbkey1 = pbkdf2(sha256, 'password', 'salt', { c: 32, dkLen: 32 });
const pbkey2 = await pbkdf2Async(sha256, 'password', 'salt', { c: 32, dkLen: 32 });
const pbkey3 = await pbkdf2Async(sha256, Uint8Array.from([1, 2, 3]), Uint8Array.from([4, 5, 6]), {
c: 32,
dkLen: 32,
});
Matches RFC 2898.
Scrypt
import { scrypt, scryptAsync } from '@noble/hashes/lib/scrypt.js';
const scr1 = scrypt('password', 'salt', { N: 2 ** 16, r: 8, p: 1, dkLen: 32 });
const scr2 = await scryptAsync('password', 'salt', { N: 2 ** 16, r: 8, p: 1, dkLen: 32 });
const scr3 = await scryptAsync(Uint8Array.from([1, 2, 3]), Uint8Array.from([4, 5, 6]), {
N: 2 ** 22,
r: 8,
p: 1,
dkLen: 32,
onProgress(percentage) {
console.log('progress', percentage);
},
maxmem: 2 ** 32 + 128 * 8 * 1,
});
Matches RFC 7914, Website
N, r, p
are work factors. To understand them, see the blog post.dkLen
is the length of output bytes- It is common to use N from
2**10
to 2**22
and {r: 8, p: 1, dkLen: 32}
onProgress
can be used with async version of the function to report progress to a user.
Memory usage of scrypt is calculated with the formula N * r * p * 128 + (128 * r * p)
, which means
{N: 2 ** 22, r: 8, p: 1}
will use 4GB + 1KB of memory. To prevent DoS, we limit scrypt to 1GB + 1KB
of RAM used,
which corresponds to {N: 2 ** 20, r: 8, p: 1}
. If you want to use higher values, increase maxmem
using the formula above.
Note: noble supports 2**22
(4GB RAM) which is the highest amount amongst JS libs. Many other implementations don't support it.
We cannot support 2**23
, because there is a limitation in JS engines that makes allocating
arrays bigger than 4GB impossible, but we're looking into other possible solutions.
utils
import { bytesToHex as toHex, randomBytes } from '@noble/hashes/lib/scrypt.js';
console.log(toHex(randomBytes(32)));
bytesToHex
will convert Uint8Array
to a hex stringrandomBytes(bytes)
will produce cryptographically secure random Uint8Array
of length bytes
Security
Noble is production-ready.
The library will be audited by an independent security firm in the next few months.
The library has been fuzzed by Guido Vranken's cryptofuzz. You can run the fuzzer by yourself to check it.
A note on timing attacks: JIT-compiler and Garbage Collector make "constant time" extremely hard to achieve in a scripting language. Which means any other JS library can't have constant-timeness. Even statically typed Rust, a language without GC, makes it harder to achieve constant-time for some cases. If your goal is absolute security, don't use any JS lib — including bindings to native ones. Use low-level libraries & languages. Nonetheless we're targetting algorithmic constant time.
We consider infrastructure attacks like rogue NPM modules very important; that's why it's crucial to minimize the amount of 3rd-party dependencies & native bindings. If your app uses 500 dependencies, any dep could get hacked and you'll be downloading rootkits with every npm install
. Our goal is to minimize this attack vector.
Speed
Benchmarks measured on Apple M1 with macOS 12.
Note that PBKDF2 and Scrypt are tested with extremely high work factor.
To run benchmarks, execute npm run bench-install
and then npm run bench
SHA256 32B x 1,126,126 ops/sec @ 888ns/op
SHA384 32B x 443,458 ops/sec @ 2μs/op
SHA512 32B x 448,631 ops/sec @ 2μs/op
SHA3-256, keccak256, shake256 32B x 183,621 ops/sec @ 5μs/op
Kangaroo12 32B x 310,077 ops/sec @ 3μs/op
Marsupilami14 32B x 278,164 ops/sec @ 3μs/op
BLAKE2b 32B x 297,353 ops/sec @ 3μs/op
BLAKE2s 32B x 507,614 ops/sec @ 1μs/op
BLAKE3 32B x 584,795 ops/sec @ 1μs/op
RIPEMD160 32B x 1,186,239 ops/sec @ 843ns/op
HMAC-SHA256 32B x 346,860 ops/sec @ 2μs/op
HKDF-SHA256 32B x 153,045 ops/sec @ 6μs/op
PBKDF2-HMAC-SHA256 262144 x 2 ops/sec @ 338ms/op
PBKDF2-HMAC-SHA512 262144 x 0 ops/sec @ 1024ms/op
Scrypt r: 8, p: 1, n: 262144 x 1 ops/sec @ 637ms/op
Compare to native node.js implementation that uses C bindings instead of pure-js code:
SHA256 32B native x 1,164,144 ops/sec @ 859ns/op
SHA384 32B native x 938,086 ops/sec @ 1μs/op
SHA512 32B native x 946,969 ops/sec @ 1μs/op
SHA3 32B native x 879,507 ops/sec @ 1μs/op
keccak, k12, m14 are not implemented
BLAKE2b 32B native x 879,507 ops/sec @ 1μs/op
BLAKE2s 32B native x 977,517 ops/sec @ 1μs/op
BLAKE3 is not implemented
RIPEMD160 32B native x 913,242 ops/sec @ 1μs/op
HMAC-SHA256 32B native x 755,287 ops/sec @ 1μs/op
HKDF-SHA256 32B native x 207,856 ops/sec @ 4μs/op
PBKDF2-HMAC-SHA256 262144 native x 23 ops/sec @ 42ms/op
Scrypt 262144 native x 1 ops/sec @ 564ms/op
Scrypt 262144 scrypt.js x 0 ops/sec @ 1678ms/op
It is possible to make this library 4x+ faster by
doing code generation of full loop unrolls. We've decided against it. Reasons:
- the library must be auditable, with minimum amount of code, and zero dependencies
- most method invocations with the lib are going to be something like hashing 32b to 64kb of data
- hashing big inputs is 10x faster with low-level languages, which means you should probably pick 'em instead
The current performance is good enough when compared to other projects; SHA256 takes only 900 nanoseconds to run.
Contributing & testing
- Clone the repository.
npm install
to install build dependencies like TypeScriptnpm run build
to compile TypeScript codenpm run test
will execute all main tests. See our approach to testingnpm run test-dos
will test against DoS; by measuring function complexity. Takes ~20 minutesnpm run test-big
will execute hashing on 4GB inputs,
scrypt with 1024 different N, r, p
combinations, etc. Takes several hours. Using 8-32+ core CPU helps.
License
The MIT License (MIT)
Copyright (c) 2021 Paul Miller (https://paulmillr.com)
See LICENSE file.